package com.wb.service.impl;

import com.wb.mapper.EbProductCategoryMapper;
import com.wb.mapper.EbProductMapper;
import com.wb.model.EbProduct;
import com.wb.model.EbProductCategory;
import com.wb.service.ProductService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 用一句话描述
 * @Auther: 新梦想*陈超
 * @Date: 2020-10-19 14:58
 */
@Service//表明是service层(底层将当前类纳入到spring ioc(bean)容器进行管理)
public class ProductServiceImpl implements ProductService {
    @Autowired
    private EbProductMapper ebProductMapper;

    @Autowired
    private EbProductCategoryMapper ebProductCategoryMapper;

    @Override
    public List<EbProduct> list(int page, int pageSize) {
        //web-service(现在)-mapper-数据库
        PageHelper.startPage(page,pageSize);
        List<EbProduct> ebProducts = ebProductMapper.selectByExample(null);
        return ebProducts;
    }

    @Override
    public int save(EbProduct ebProduct) {
        //1.获取小类的id
        int smallId = ebProduct.getEpcChildId();
        //2.根据小类该小类的所有信息
        EbProductCategory categoryObj = ebProductCategoryMapper.selectByPrimaryKey(smallId);
        //3.获取该小类的父类的id
        Integer epcParentId = categoryObj.getEpcParentId();
        //4.将父类id绑定到添加商品对象上
        ebProduct.setEpcId(epcParentId);

        return ebProductMapper.insertSelective(ebProduct);
    }

    @Override
    public EbProduct selectOne(Integer epId) {
        return ebProductMapper.selectByPrimaryKey(epId);
    }

    @Override
    public int update(EbProduct ebProduct) {
        //1.获取小类的id
        int smallId = ebProduct.getEpcChildId();
        //2.根据小类该小类的所有信息
        EbProductCategory categoryObj = ebProductCategoryMapper.selectByPrimaryKey(smallId);
        //3.获取该小类的父类的id
        Integer epcParentId = categoryObj.getEpcParentId();
        //4.将父类id绑定到添加商品对象上
        ebProduct.setEpcId(epcParentId);
        return ebProductMapper.updateByPrimaryKeySelective(ebProduct);
    }




}
